package com.chixing.service.impl;

import com.chixing.pojo.Support;
import com.chixing.service.RedisLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisLikeServiceImpl implements RedisLikeService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 点赞数据保存到redis中
     * 1. 保存用户点赞记录 到redis (customer_liked_record    10004::50036 1 )
     * 2. 统计评论的总点赞数  (review_praise_count  50036::5)
     */
    @Override
    public void saveLike2Redis(Support support, Integer supportCount ) {
        //1. 存储 点赞数据 到redis (10004::50036 1 )
        // redis : "customer_liked_record"  10004::50036 1
        String key =support.getMemberId() + "::"+ support.getReviewId();
        Integer value = support.getSupportStatus();
        System.out.println("key:"+key);
        System.out.println("value:"+value);
        redisTemplate.opsForHash().put("save_praise_record",key,value) ;  //10004::50036 1


        //2. 统计评论的总点赞数  ("review_praise_count"  50036::1)
        //2. 统计评论的总点赞数  (                       50037::1)
        String reviewCountkey = support.getReviewId().toString();
        System.out.println("reviewCountkey:" + reviewCountkey);
        if(!redisTemplate.hasKey("review_praise_count")){
            redisTemplate.opsForHash().put("review_praise_count",reviewCountkey,supportCount);
        }else{

            if(!redisTemplate.opsForHash().hasKey("review_praise_count",reviewCountkey)){

                redisTemplate.opsForHash().put("review_praise_count",reviewCountkey,supportCount);
            }else{
                Integer reviewTotalCountValue =  (Integer)redisTemplate.opsForHash().get("review_praise_count",reviewCountkey);
                reviewTotalCountValue++;
                System.out.println("reviewTotalCountValue:" + reviewTotalCountValue);
                redisTemplate.opsForHash().put("review_praise_count",reviewCountkey,supportCount);
            }


        }
    }

}
